//
//  LBViewController.m
//  LBHeaderExpandLib
//
//  Created by liubo on 11/19/2022.
//  Copyright (c) 2022 liubo. All rights reserved.
//

#import "LBViewController.h"
#import <VVScrollViewHelper.h>

#define ScreenWidth [UIScreen mainScreen].bounds.size.width
#define ScreenHeight [UIScreen mainScreen].bounds.size.height

@interface LBViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView  *tableView;


@property (nonatomic, strong) VVScrollViewHelper *headerHelper;

@property (nonatomic, strong) UIImageView *imgView;

@property (nonatomic, strong) UIView *headerContentView;


@end

@implementation LBViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.tableView.backgroundColor = [UIColor redColor];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableViewCell class])];
    [self.view addSubview:self.tableView];
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.sectionHeaderTopPadding = 0;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    self.tableView.backgroundColor = [UIColor redColor];
    [self headerHelper];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    return 100;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell  *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class])];
    cell.contentView.backgroundColor = [UIColor cyanColor];
    if (indexPath.row == 0) {
        CAShapeLayer *layer = [[CAShapeLayer alloc] init];
        UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, ScreenWidth, 60) byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(40, 40)];
        layer.path = path.CGPath;
        cell.layer.mask = layer;
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60 ;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [self.headerHelper scrollViewDidScroll:scrollView];
}

#pragma mark - lazy load

- (UIImageView *)imgView
{
    if (!_imgView) {
        _imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenWidth * 5 / 8)];
        _imgView.image = [UIImage imageNamed:@"kkkk"];
        _imgView.contentMode = UIViewContentModeScaleAspectFill;
        _imgView.backgroundColor = [UIColor cyanColor];
    }
    return _imgView;
}

- (UIView *)headerContentView
{
    if (!_headerContentView) {
        _headerContentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenWidth * 5 / 8)];
        _headerContentView.backgroundColor = [UIColor clearColor];
    }
    return _headerContentView;
}

- (VVScrollViewHelper *)headerHelper
{
    if (!_headerHelper) {
        VVScrollExtraViewConfig  *config = [[VVScrollExtraViewConfig alloc] init];
        config.backgroundView = self.imgView;
        config.frontView = self.headerContentView;
        config.headerOverHeight = 40;
        _headerHelper = [VVScrollViewHelper initWithScrollView:self.tableView headerViewConfig:config];
    }
    return _headerHelper;
}

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth , ScreenHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

@end
